package com.bwie.service.impl;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.client.CouponClient;
import com.bwie.client.ItemClient;
import com.bwie.pay.AlipayUtils;
import com.bwie.pay.WxPayUtils;
import com.bwie.pojo.TbOrder;
import com.bwie.service.TbOrderService;
import com.bwie.mapper.TbOrderMapper;
import com.bwie.utils.Result;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 28242
* @description 针对表【tb_order】的数据库操作Service实现
* @createDate 2025-07-11 19:43:42
*/
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder>
    implements TbOrderService{
    @Autowired
   private TbOrderMapper tbOrderMapper;


    @Autowired
    private ItemClient itemClient;

    @Autowired
    private CouponClient couponClient;


    @Autowired
    HttpServletResponse response;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result SaveOrder(TbOrder tbOrder) {

        String nowDate = new SimpleDateFormat("yyMMdd").format(new Date());
        String format = null;
        try {
            Jedis jedis = new Jedis("192.168.80.192", 6379);
            String userId="user";
            Long incr = jedis.incr(userId);
            format = String.format("%013d", incr);
            jedis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        tbOrder.setOrderCode(nowDate+format);

        BigDecimal bigDecimal = itemClient.queryItemPriceByItemId(tbOrder.getItemId());
        tbOrder.setItemPrice(bigDecimal);

        couponClient.SaveCoupon(tbOrder.getCustId());



        tbOrder.setCreateTime(new Date());
        tbOrder.setOrderStatus(0);
        tbOrder.setDeleted(0);
        tbOrderMapper.insert(tbOrder);
        return Result.ok();
    }

    @Override
    public Result OrderList() {

     List<TbOrder> orders= tbOrderMapper.queryOrderList();


        return Result.ok().setData(orders);
    }

    @Override
    public void updadatePayStatus(String out_trade_no) {

        TbOrder tbOrder = tbOrderMapper.selectOne(
                new QueryWrapper<TbOrder>().lambda().eq(TbOrder::getOrderCode, out_trade_no)
        );
        tbOrder.setOrderStatus(1);
        tbOrderMapper.updateById(tbOrder);

    }

    @Override
    public void PayOrder(String orderCode) throws IOException {

        TbOrder tbOrder = tbOrderMapper.selectOne(
                new QueryWrapper<TbOrder>().lambda().eq(TbOrder::getOrderCode, orderCode)
        );
        if (tbOrder==null){
          throw  new RuntimeException("订单不存在");
        }

        BigDecimal itemPrice = tbOrder.getItemPrice();


        String s = WxPayUtils.postOrderObj(orderCode, "订单", itemPrice);


        response.setContentType("image/jpg");


           QrCodeUtil.generate(s,130,130,"png",response.getOutputStream());



    }


}




